package ck.mmd.action;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import ck.mmd.bean.Adopt;
import ck.mmd.bean.News;
import ck.mmd.bean.Remark;
import ck.mmd.bean.User;
import ck.mmd.dao.AdoptDao;
import ck.mmd.dao.NewsDao;
import ck.mmd.dao.RemarkDao;
import ck.mmd.dao.TypeDao;
import ck.mmd.dao.UserDao;
import ck.mmd.util.StringUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport  implements SessionAware,ModelDriven<User>{
	private static final long serialVersionUID = 1L;  
	private User user;
	private UserDao userdao = new UserDao();
	private RemarkDao remarkdao = new RemarkDao();
	private AdoptDao adoptdao = new AdoptDao();
	private NewsDao newsdao = new NewsDao();
	private TypeDao typedao =  new TypeDao();
	HttpServletRequest request = ServletActionContext.getRequest();
	HttpServletResponse response = ServletActionContext.getResponse();
	
	public User getModel() {
		if(user == null){
			return user = new User();
		}
		return user;
	}
	
	/**
     * 校验验证码是否正确
     * @return 返回登录视图
     */
	public String ajaxValidateCode() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		
		//获取输入的校验码
		String code = request.getParameter("valicode");
		
		//获取真正的校验码
		String vali = (String) request.getSession().getAttribute("securityCode");
				
		boolean b = code.equals(vali);
		//发送给客户端
		
		response.getWriter().print(b);
		return null;
	} 
	
	/**
	 * 登录
	 * @return 返回登录之后视图
	 * @throws IOException 
	 */
	public String login() throws IOException{		
		HttpServletResponse response = ServletActionContext.getResponse();
		//调用userdao 里面登录的方法
		User newuser = userdao.login(user);
		String error = "";
		//如果查找用户为空，则账号密码错误!
		if(newuser == null || newuser.equals("")){
			error="1";
		}else{
			//如果用户还没激活
			if(newuser.getActivestate() == 0){
				error="2";
			}else{
				error="0";
				//跟新最后一次登录时间
				Date date = new Date();
				userdao.updateLoginTime(date,newuser.getId());
				//session存储登录的用户所有信息，并且信息封装在newuser中。
				ActionContext.getContext().getSession().put("user", newuser);
			}
		}
		//error响应到页面，
		response.getWriter().print(error);
		return null;
	}
	
	/**
	 * 注册
	 * @return 返回视图
	 * @throws IOException 
	 */
	public String regist() throws IOException {
		String name = user.getUsername();
		//判断用户名是否存在
		User user2 = userdao.findUserByName(name);
		String error = "";
		if(user2 != null){
			error = "1";
		}else{
			User newuser = user;
			Date date = new Date();
			//设置id  32位
			newuser.setId(StringUtils.getUUID());
			newuser.setRegisttime(date);
			newuser.setActivecode("");
			newuser.setActivestate(1);
			newuser.setPriced(0);
			newuser.setPower(1);
			newuser.setPhone("");
			newuser.setIssendsale(0);
			newuser.setIssale_power(0);
			newuser.setBackimg("backimg_7.jpg");
			newuser.setSalename("");
			newuser.setSaletype("0");
			newuser.setSendtime(null);
			newuser.setSendoktime(null);
			newuser.setLastlogintime(null);
			newuser.setSex("男");
			newuser.setRealname("");
			newuser.setLogoimg("mylogo_1.jpg");
			//如果查找用户为空，则添加数据库失败
			userdao.addUser(newuser);
			User userdemo = userdao.findUserById(newuser.getId());
			if(userdemo == null || userdemo.equals("")){
				error="2";
			}else{
				error="0";
			}
		}
		
		response.getWriter().print(error);
		return null;
	}
	
	/**
	 * 注销
	 * @return
	 * @throws IOException
	 */
	public String loginout() throws IOException{
		ActionContext.getContext().getSession().remove("user");
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		String error = "";
		if(newuser != null){
			//注销失败
			error = "0";
		}else{
			error = "1";
		}
		response.getWriter().print(error);
		return null;
	}
	
	/**
	 * 我的空间
	 * @return
	 * @throws IOException
	 */
	public String myspace() throws IOException{
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			return "myspace";
		}
		
	}
	
	/**
	 * 我的领养
	 * @return
	 * @throws IOException
	 */
	public String myadopt() throws IOException{
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			if(newuser.getIssale_power() == 1){
				//已经申请过
				return "mysale";
			}else{
				//正在审核当中
				//未申请
				return "mytorzsale";	
			}
		}
	}
	
	/**
	 * 认证界面
	 * @return
	 * @throws IOException
	 */
	public String myrzsale() throws IOException{
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			if(newuser.getIssale_power() == 1){
				//已经申请过
				return "mysale";
			}else{
				//正在审核当中
				//未申请
				return "mytorzsale";
			}
		}
	}
	
	/**
	 * 进入领养具体信息   已认证进入认证主页  ，没有则进入申请认证界面
	 * @return
	 * @throws IOException
	 */
	public String mysale() throws IOException{
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			if(newuser.getIssale_power() == 1){
				//已经申请过
				return "mysale";
			}else{
				if(newuser.getIssendsale() == 1){
					//正在审核当中
					return "mytorzsale";
				}else{
					//未申请
					return "myrzsale";
				}
				
			}
		}
	}
	
	//添加申请认证信息
	public String addrzinfo() throws IOException{
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		String error = "";
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			error = "0";
			//获取信息
			String saletype = request.getParameter("saletype");
			String realname = request.getParameter("realname");
			String salename = request.getParameter("salename");
			String phone = request.getParameter("phone");
			User user = newuser;
			user.setSaletype(saletype);user.setRealname(realname);user.setSalename(salename);
			user.setPhone(phone);user.setIssendsale(1);user.setId(newuser.getId());
			user.setSendtime(new Date());
			
			//添加到数据库
			userdao.addRzInfo(user);
			User u = userdao.findUserById(user.getId());
			if(u.getIssendsale() == 1){
				error = "0";
				ActionContext.getContext().getSession().put("user", user);
			}else{
				error = "1";
			}
			response.getWriter().print(error);
		}
		return null;
	}
	
	/**
	 * 我的评论
	 * @throws IOException 
	 */
	public String myremark() throws IOException{
		ActionContext context = ActionContext.getContext();
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String isnull = "1";
			List<Remark> remarklist = new ArrayList<Remark>();
			remarklist = remarkdao.findRemarksByUserId(newuser.getId());
			if(remarklist.size()<=0){
				isnull = "0";
				context.put("isnull", isnull);
			}else{
				isnull = "1";
				context.put("list", remarklist);
				context.put("isnull", isnull);
			}
			
		}
		return "myremark";
	}
	
	/**
	 * 删除评论
	 * @throws IOException 
	 */
	public String deleteRemark() throws IOException{
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		String error = "";
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String id = request.getParameter("id");
			if(id.equals("") || id == null){
				error = "1";
			}else{
				error = "0";
				remarkdao.deleteById(id);
			}
			response.getWriter().print(error);
		}
		return null;
	}
	
	/**
	 * 发表评论
	 * @return
	 * @throws IOException
	 */
	public String add_remark() throws IOException{
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		String error = "";
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String last_id = request.getParameter("last_id");
			String user_id = request.getParameter("user_id");
			String content = request.getParameter("content");
			String last_title = request.getParameter("last_title");
			String key = request.getParameter("key");
			if(last_id.equals("") || last_id == null){
				error = "1";
			}else{
				error = "0";
				Remark remark = new Remark();
				remark.setContent(content);
				remark.setLast_id(last_id);
				remark.setLast_title(last_title);
				remark.setUser_id(user_id);
				remark.setUser_name(userdao.findUserById(user_id).getUsername());
				remark.setUptime(new Date());
				remark.setId(StringUtils.getUUID());
				remark.setLikenum(0);
				remarkdao.addRemark(remark);
				//更新对应的评论数量和热度
				if(key.equals("article")){
					News news = newsdao.findNewsById(last_id);
					newsdao.updateRemarkNumAndHotNum(last_id,news.getRemarknum()+1,news.getHotnum()+5);
				}else if(key.equals("adopt")){
					Adopt adopt = adoptdao.findAdoptById(last_id);
					adoptdao.updateRemarkNum(last_id,adopt.getRemarknum()+1);
				}
			}
			response.getWriter().print(error);
		}
		return null;
	}
	
	/**
	 * 管理用户列表
	 * @return
	 * @throws IOException
	 */
	public String my_users() throws IOException{
		ActionContext context = ActionContext.getContext();
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String isnull = "1";
			List<User> users = new ArrayList<User>();
			users = userdao.findAllUser();
			if(users.size()<=0){
				isnull = "0";
				context.put("isnull", isnull);
			}else{
				isnull = "1";
				context.put("list", users);
				context.put("isnull", isnull);
			}
		}
		return "my_users";
	}
	
	/**
	 * 所有管理员
	 * @return
	 * @throws IOException
	 */
	public String my_users_admin() throws IOException{
		ActionContext context = ActionContext.getContext();
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String isnull = "1";
			List<User> users = new ArrayList<User>();
			users = userdao.findAllUserByAdmin();
			if(users.size()<=0){
				isnull = "0";
				context.put("isnull", isnull);
			}else{
				isnull = "1";
				context.put("list", users);
				context.put("isnull", isnull);
			}
		}
		return "my_users";
	}
	
	/**
	 * 所有认证的用户
	 * @return
	 * @throws IOException
	 */
	public String my_users_sended() throws IOException{
		ActionContext context = ActionContext.getContext();
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String isnull = "1";
			List<User> users = new ArrayList<User>();
			users = userdao.findAllBySended();
			if(users.size()<=0){
				isnull = "0";
				context.put("isnull", isnull);
			}else{
				isnull = "1";
				context.put("list", users);
				context.put("isnull", isnull);
			}
		}
		return "my_users";
	}
	
	/**
	 * 普通用户
	 * @return
	 * @throws IOException
	 */
	public String my_users_normal() throws IOException{
		ActionContext context = ActionContext.getContext();
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String isnull = "1";
			List<User> users = new ArrayList<User>();
			users = userdao.findAllByNormal();
			if(users.size()<=0){
				isnull = "0";
				context.put("isnull", isnull);
			}else{
				isnull = "1";
				context.put("list", users);
				context.put("isnull", isnull);
			}
		}
		return "my_users";
	}
	
	/**
	 * 删除用户
	 * @throws IOException 
	 */
	public String deleteUser() throws IOException{
		String error = "0";
		
		response.getWriter().print(error);
		return null;
	}
	
	/**
	 * 管理文章
	 * @return
	 * @throws IOException
	 */
	public String my_articles() throws IOException{
		ActionContext context = ActionContext.getContext();
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String isnull = "1";
			List<News> newslist = new ArrayList<News>();
			newslist = newsdao.findAllNews();
			if(newslist.size()<=0){
				isnull = "0";
				context.put("isnull", isnull);
			}else{
				isnull = "1";
				Iterator<News> it =  newslist.iterator();
				List<News> newslistdemo = new ArrayList<News>();
				while(it.hasNext()){
					News news = it.next();
					news.setType(typedao.findTypeById(news.getType_id()));
					newslistdemo.add(news);
				}
				context.put("list", newslistdemo);
				context.put("isnull", isnull);
			}
		}
		return "my_articles";
	}
	
	/**
	 * 管理文章-热度排序
	 * @return
	 * @throws IOException
	 */
	public String my_articles_hot() throws IOException{
		ActionContext context = ActionContext.getContext();
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String isnull = "1";
			List<News> newslist = new ArrayList<News>();
			newslist = newsdao.findAllNewsByHot();
			if(newslist.size()<=0){
				isnull = "0";
				context.put("isnull", isnull);
			}else{
				isnull = "1";
				Iterator<News> it =  newslist.iterator();
				List<News> newslistdemo = new ArrayList<News>();
				while(it.hasNext()){
					News news = it.next();
					news.setType(typedao.findTypeById(news.getType_id()));
					newslistdemo.add(news);
				}
				context.put("list", newslistdemo);
				context.put("isnull", isnull);
			}
		}
		return "my_articles";
	}
	
	/**
	 * 管理文章-新闻
	 * @return
	 * @throws IOException
	 */
	public String my_articles_news() throws IOException{
		ActionContext context = ActionContext.getContext();
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String isnull = "1";
			List<News> newslist = new ArrayList<News>();
			newslist = newsdao.findAllNewsByType("10001");
			if(newslist.size()<=0){
				isnull = "0";
				context.put("isnull", isnull);
			}else{
				isnull = "1";
				Iterator<News> it =  newslist.iterator();
				List<News> newslistdemo = new ArrayList<News>();
				while(it.hasNext()){
					News news = it.next();
					news.setType(typedao.findTypeById(news.getType_id()));
					newslistdemo.add(news);
				}
				context.put("list", newslistdemo);
				context.put("isnull", isnull);
			}
		}
		return "my_articles";
	}
	
	/**
	 * 管理文章-知识
	 * @return
	 * @throws IOException
	 */
	public String my_articles_know() throws IOException{
		ActionContext context = ActionContext.getContext();
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String isnull = "1";
			List<News> newslist = new ArrayList<News>();
			newslist = newsdao.findAllNewsByType("10002");
			if(newslist.size()<=0){
				isnull = "0";
				context.put("isnull", isnull);
			}else{
				isnull = "1";
				Iterator<News> it =  newslist.iterator();
				List<News> newslistdemo = new ArrayList<News>();
				while(it.hasNext()){
					News news = it.next();
					news.setType(typedao.findTypeById(news.getType_id()));
					newslistdemo.add(news);
				}
				context.put("list", newslistdemo);
				context.put("isnull", isnull);
			}
		}
		return "my_articles";
	}
	
	/**
	 * 实名申请
	 * @return
	 * @throws IOException
	 */
	public String my_usersend() throws IOException{
		ActionContext context = ActionContext.getContext();
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String isnull = "1";
			List<User> userlist = new ArrayList<User>();
			userlist = userdao.findAllUserBySend();
			if(userlist.size()<=0){
				isnull = "0";
				context.put("isnull", isnull);
			}else{
				isnull = "1";
				context.put("list", userlist);
				context.put("isnull", isnull);
			}
		}
		return "my_usersend";
	}
	
	/**
	 * 实名申请
	 * @return
	 * @throws IOException
	 */
	public String my_usersended() throws IOException{
		ActionContext context = ActionContext.getContext();
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String isnull = "1";
			List<User> userlist = new ArrayList<User>();
			userlist = userdao.findAllUserBySended();
			if(userlist.size()<=0){
				isnull = "0";
				context.put("isnull", isnull);
			}else{
				isnull = "1";
				context.put("list", userlist);
				context.put("isnull", isnull);
			}
		}
		return "my_usersend";
	}
	
	
	
	/**
	 * 管理反馈
	 * @return
	 * @throws IOException
	 */
	public String my_messages() throws IOException{
		ActionContext context = ActionContext.getContext();
		User newuser = new User();
		newuser = (User)ActionContext.getContext().getSession().get("user");
		
		if(newuser == null){
			PrintWriter out = response.getWriter();
			out.print("<script language='JavaScript'> alert('你未登录!');  " +
					" location.href='"+request.getContextPath()+"/';</script>");
			return null;
		}else{
			String isnull = "1";
			List<News> newslist = new ArrayList<News>();
			newslist = newsdao.findAllNewsByType("10001");
			if(newslist.size()<=0){
				isnull = "0";
				context.put("isnull", isnull);
			}else{
				isnull = "1";
				context.put("list", newslist);
				context.put("isnull", isnull);
			}
		}
		return "my_messages";
	}
	
	public void setSession(Map<String, Object> arg0) {
		
	}
}
